package com.threaddemo4;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class ThenCombineDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<String> f1 = CompletableFuture.supplyAsync(()->{
            System.out.println("F2 " + Thread.currentThread().getName());
            return "F1 返回值";
        });

        CompletableFuture<String> f2 = CompletableFuture.supplyAsync(()->{
            System.out.println("F1 " + Thread.currentThread().getName());
            return "F2 返回值";
        });

        //等f1、f2全部执行后 再去执行
        //a、b对应f1、f2的返回值
        //thenCombine有返回值
        CompletableFuture<String> tc = f1.thenCombine(f2,(a,b)->{
            System.out.println("a = " + a);
            System.out.println("b = " + b);
            return "then Combine 返回值";
        });

        //等f1、f2全部执行后 再去执行
        //a、b对应f1、f2的返回值
        //thenAcceptBoth 没有返回值
        CompletableFuture tab = f1.thenAcceptBoth(f2,(a,b)->{
            System.out.println("a = " + a);
            System.out.println("b = " + b);
        });

        //等f1、f2全部执行后 再去执行
        //没有参数 也没有返回值
        CompletableFuture rab = f1.runAfterBoth(f2,()->{
            System.out.println("执行run After Both");
        });


        System.out.println("f1 = " + f1.get());
        System.out.println("f2 = " + f2.get());
        System.out.println("tc = " + tc.get());
        System.out.println("tab = " + tab.get());
        System.out.println("rab = " + rab.get());

    }
}
